package com.lwei.ljpro.service.impl;

import com.lwei.ljpro.entry.UserEntry;
import com.lwei.ljpro.service.IUserService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserEntry> implements IUserService {
    @Override
    protected String getKey() {
        return "userEntry";
    }

    @Override
    public Long saveUser(UserEntry userEntry) {
        if(userEntry == null){
            return null;
        }
        UserEntry user = checkUserByTel(userEntry.getLoginTel());
        if(user == null){
            return super.save(userEntry);
        } else {
            super.update(user.getId(), user);
            return user.getId();
        }
    }

    @Override
    public UserEntry checkUserByTel(String tel) {
        List<UserEntry> userList = super.listEntryAll(getKey());
        for (UserEntry user : userList) {
            if(tel.equals(user.getLoginTel())){
                return user;
            }
        }
        return null;
    }

    @Override
    public void updateUserLastAliveTime(Long id) {
        UserEntry userEntry = super.getEntryById(id);
        userEntry.setLastTime(new Date());
        super.update(id, userEntry);
    }

    @Override
    public List<UserEntry> listUserAll() {
        return super.listEntryAll(getKey());
    }
}
